package com.lbyl.adapter.topnews;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lbyl.Service.BitmapService;
import com.lbyl.activitiy.R;
import com.lbyl.domin.TopNewsItem.Contentlist;

import java.util.ArrayList;
import java.util.List;

/**
 * Topnews内容页listview适配器
 *
 * @author libin996@outlook.com
 */

public class TopNewsListViewAdapter extends BaseAdapter {
    private static final String TAG = "TopNewsListViewAdapter";
    private Context mContext;
    private ArrayList<Contentlist> dataList;//所有数据
    private BitmapService bitmapService;

    public TopNewsListViewAdapter(Context cxt, ArrayList<Contentlist> dataList) {
        this.mContext = cxt;
        this.dataList = dataList;
        bitmapService = new BitmapService();
    }


    @Override
    public int getCount() {
        if (dataList == null) {
            return 0;
        }
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        Log.d(TAG, "getItem() called with: position = [" + position + "]");
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        Log.d(TAG, "getItemId() called with: position = [" + position + "]");
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "getView() called with: position = [" + position + "], convertView = [" + convertView + "], parent = [" + parent + "]");


        String imgUrl = "";
        ViewHolder holder;
//        if (convertView != null) {
//            return convertView;
//        }//由于需要刷新,不能重用缓存数据
        //准备控件


        if (convertView != null) {//如果本地加载过并自动保存在了converView 中,holder就指向convertView
            holder = (ViewHolder) convertView.getTag();
        } else {//convertview为null
            convertView = View.inflate(mContext, R.layout.item_pager_content_topnews, null);//初始化itemview
            holder = new ViewHolder();
            holder.textView_title = (TextView) convertView.findViewById(R.id.textview_item_title_pager_topnews);
            holder.textView_desc = (TextView) convertView.findViewById(R.id.textView__item_desc_pager_topnews);
            holder.imageView_image = (ImageView) convertView.findViewById(R.id.imageview_item_img_pager_topnews);
            convertView.setTag(holder);
        }

        //设置数据
        Contentlist data = (Contentlist) getItem(position);
        holder.textView_title.setText(data.getTitle());
        holder.textView_desc.setText(data.getDesc());

        //加载图片
        if (data.getImageurls().size() != 0) {//如果有图片
            String tmpImgUrl = data.getImageurls().get(0).getUrl();
            holder.imageView_image.setTag(tmpImgUrl);
            imgUrl = tmpImgUrl;
            if (holder.imageView_image.getDrawable() == null) {//如果能显示图片但还没显示,就显示出默认图片
                holder.imageView_image.setImageResource(R.mipmap.ic_launcher);

            }
        }


        //三级缓存工具显示图片
        bitmapService.display(holder.imageView_image, imgUrl);
        return convertView;
    }

    /**
     * 外部添加items.初始化或者上拉更新的时候调用
     */
    public void addItems(List<Contentlist> data) {

        if (this.dataList == null) {
            dataList = new ArrayList<>();
        }
        Log.i(TAG, "addItems: 传入的data的长度是" + data.size());
        this.dataList.addAll(data);

        this.notifyDataSetChanged();
        Log.i(TAG, "addItems: 适配器刷新之后的长度是:" + this.dataList.size());
    }

    /**
     * 下滑更新items,直接创建新的list
     */
    public void refreshItems(ArrayList<Contentlist> data) {
        dataList = data;
        this.notifyDataSetChanged();
    }


    public static class ViewHolder {
        public TextView textView_title;
        public TextView textView_desc;
        public ImageView imageView_image;
    }


}
